﻿using System;
using System.Text;
using System.Web.UI.WebControls;

using Sunyi.Project.WebPoint.BusinessLogic;
using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.Configuration;
using Sunyi.Project.WebPoint.DataContract;
using Sunyi.Project.WebPoint.Resource;
using Sunyi.Project.WebPoint.WebControl;

using ActivitySystem.BusinessLogic;
using ActivitySystem.Common;
using ActivitySystem.DataContract;

namespace ActivitySystem.Web.Activity
{
    public partial class TypeEdit : WebPointPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    if (!string.IsNullOrEmpty(Request.Params[ActivitySystemGlobalConstant.TypeId]))
                    {
                        Guid typeId = new Guid(Request.Params[ActivitySystemGlobalConstant.TypeId]);
                        BindType(typeId);
                    }
                }
                catch (Exception ex)
                {
                    ExceptionHandler.Handle(this, ex);
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string validationMessage = ValidateParameters();

            if (!string.IsNullOrEmpty(validationMessage))
            {
                this.ShowClientMessage(validationMessage);
                return;
            }

            ActivityType activityType = null;
            bool isTypeExist = false;
            ActivityTypeManager activityTypeManager = new ActivityTypeManager();

            try
            {
                if (!string.IsNullOrEmpty(tbxTypeId.Text))
                {
                    activityType = activityTypeManager.Select(new Guid(tbxTypeId.Text));
                    if (activityType != null) isTypeExist = true;
                }
                else
                {
                    activityType = activityTypeManager.SelectByName(tbxTypeName.Text.Trim());

                    if (activityType == null)
                    {
                        activityType = new ActivityType();
                        activityType.Id = Guid.NewGuid();
                    }
                    else
                    {
                        isTypeExist = true;
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(this, ex);
                return;
            }

            activityType.Name = tbxTypeName.Text.Trim();
            activityType.Description = tbxDescription.Text;
            activityType.IsActive = Boolean.Parse(ddlStatus.SelectedValue);

            try
            {
                if (isTypeExist)
                {
                    activityTypeManager.Update(activityType);

                    AuditLogger.Write(AuditLogOperationCode.Update, this.GetType().Name, activityType);

                    ClientRedirect(Request.UrlReferrer.AbsoluteUri);
                }
                else
                {
                    activityTypeManager.Insert(activityType);

                    AuditLogger.Write(AuditLogOperationCode.Create, this.GetType().Name, activityType);

                    ClientRedirect("TypeList.aspx");
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(this, ex);
            }
        }

        #region Private Method

        private void BindType(Guid typeId)
        {
            ActivityTypeManager activityTypeManager = new ActivityTypeManager();
            ActivityType activityType = activityTypeManager.Select(typeId);

            if (activityType != null)
            {
                tbxTypeId.Text = activityType.Id.ToString();
                tbxTypeName.Text = activityType.Name;
                tbxDescription.Text = activityType.Description;
                WebPointWebCommon.SetItemSelected(ddlStatus, activityType.IsActive.ToString());
            }
        }

        private string ValidateParameters()
        {
            StringBuilder sb = new StringBuilder();

            if (string.IsNullOrEmpty(tbxTypeName.Text))
            {
                tbxTypeName.BackColor = WebControlCommon.WarningBackColor;
                sb.Append(string.Format(WebResource.FieldValueIsNull, tbxTypeName.ID) + WebPointGlobalConstant.WebEnter);
            }
            else
            {
                tbxTypeName.BackColor = WebControlCommon.DefaultControlBackColor;
            }

            return sb.ToString();
        }

        #endregion

        #region WebPointPage Member

        protected override void ApplyPermission()
        {
            if (!string.IsNullOrEmpty(Request.Params[WebPointGlobalConstant.RoleId]) && !this.HasPermission(PermissionType.Update))
            {
                btnSubmit.Visible = false;
            }

            if (string.IsNullOrEmpty(Request.Params[WebPointGlobalConstant.RoleId]) && !this.HasPermission(PermissionType.Create))
            {
                btnSubmit.Visible = false;
            }

            if (this.RequestPermissionType == PermissionType.Read)
            {
                btnSubmit.Visible = false;
                tbxTypeName.Enabled = false;
                tbxDescription.Enabled = false;
                ddlStatus.Enabled = false;
            }
        }

        #endregion
    }
}